package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.BankManage;
import com.turing.post.organization.util.BankManageDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * @author zxp
 * @Apr 9, 2013
 */
@Stateless(mappedName = "organization.impl.BankManageDaoImpl")
@Remote
@Local
public class BankManageDaoImpl implements BankManageDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public BankManage getBankManageByBankManageId(BigInteger bankManageId) {
		return _em.find(BankManage.class, bankManageId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankManage> getBankManageList(Integer page) {
		if(page.intValue() == -1){
			String sql = "select * from BankManage t order by bankManageId";
			Query query = _em.createNativeQuery(sql, BankManage.class);
			return query.getResultList();
		}else{
			int maxResult = Config.PAGE_SIZE;
			int startPosition = maxResult * page;
			String sql = "select * from BankManage t order by bankManageId";
			Query query = _em.createNativeQuery(sql, BankManage.class)
					.setFirstResult(startPosition).setMaxResults(maxResult);
			return query.getResultList();
		}
	}

	@Override
	public Integer getBankManageTotal() {
		String sql = "select nvl(count(*),0) from BankManage";
		Query query = _em.createNativeQuery(sql);
		return Integer.parseInt((query.getSingleResult().toString()));
	}

	@Override
	public void insert(BankManage bmg) throws PCPSApplicationException {
		GeneralDao.insert(_em, bmg);

	}

	@Override
	public void update(BankManage bmg) throws PCPSApplicationException {
		GeneralDao.update(_em, bmg);
	}
}
